<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/paging.php');
	require_once('../_helper/helper.php');
	require_once('../_model/brand_model.php');
	
	$pageCMS = PAGE_CMS_BRAND;
	$keyword ='';
	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=brand");
		exit();
	}
	
	if (!isset($_GET['action']))
		$_GET['action'] = "list";
		
	if (isset($_GET['id'])){
		$brand_id = $_GET['id'];
	}
	if (isset($_GET['keyword'])){
		$keyword = $_GET['keyword'];
	}
	$action = $_GET['action'];
	
	
	if($action == 'search' || $action =='list')
	{
		if(isset($_GET['page']))
			$curPage = $_GET['page'];
		else
		{
			$curPage = 0;
			unset($_SESSION['numBrand']);
		}
			
			
		$brand_arr = GetBrandSearch($curPage, PAGING_NUM_ROW, $keyword);
		/*var_dump($type_arr);die();*/
		if( !isset($_SESSION['numBrand']))
		{
			$numBrand = GetNumBrandSearch($keyword);
			$_SESSION['numBrand'] = $numBrand;
		}
		else
			$numBrand = $_SESSION['numBrand'];
		$param = "action=search&keyword={$keyword}";
		/*var_dump($_SESSION);*/
		//Xu ly
		
		require_once('template/brand/brand_list.php');
		exit();
	}
	
	if($action == 'add')
	{
		if(!isset($_POST['submit'])){
			require_once('template/brand/brand_add.php');
			exit();
		}
		else{
			
			if($_POST['brand_name'] != '')
			{
				$img_url = Upload($_FILES, time().'-', "/images/brand/");
				if($img_url == null)
					$img_url = "../images/brand/logo_default.png";
				
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['brand_info_short'] = str_replace($order,'',$_POST['brand_info_short']);
				$_POST['brand_info_full'] = str_replace($order,'',$_POST['brand_info_full']);
				
				InsertBrand($_POST, $img_url);
				header("Location: /wks/brand.php?action=list");
			}
		}
		exit();
	}
	
	if($action == 'edit')
	{
		if (!isset($brand_id))
			header("Location: /wks/brand.php");		
			
		if(!isset($_POST['submit'])){
			$brand_value = GetBrandById($brand_id);
			require_once('template/brand/brand_edit.php');
			exit();
		}
		else{
			if($_POST['brand_name'] != ''){
				$img_url = Upload($_FILES, time().'-', "/images/brand/");
				// var_dump($img_url); die();
				
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['brand_info_short'] = str_replace($order,'',$_POST['brand_info_short']);
				$_POST['brand_info_full'] = str_replace($order,'',$_POST['brand_info_full']);
				
				EditBrand($brand_id, $_POST, $img_url);
				header("Location: /wks/brand.php?action=list");
			}
		}
		exit();
	}
	
	if($action == 'del')
	{
		$result = FALSE;
		if (isset($brand_id))
			$result = DeleteBrand($brand_id);
			
		if($result == FALSE)
			header("Location: /wks/brand.php?error=1&e_action=del");	
		else
			header("Location: /wks/brand.php");	
	}
	

?>


